package cn.zzzhy.spider;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zzzhy
 * @since 16/2/29 下午1:27
 */
public class DownloadPool {

    private static final int DEFAULT_POOL_SIZE = 3;

    public static final HashSet<String> download = new HashSet<>();
    private final List<DownloadTool> tools = new ArrayList<>();
    public static AtomicInteger fileNo = new AtomicInteger(0);

    public DownloadPool() {
        this(DEFAULT_POOL_SIZE);
    }

    public DownloadPool(Integer size) {
        if(size == null || size < 0){
            size = DEFAULT_POOL_SIZE;
        }
        for (int i = 0; i < size; i++) {
            DownloadTool tool = new DownloadTool();
            tools.add(tool);
        }
    }

    public void start() {
        tools.stream().forEach(DownloadTool::download);
    }

    public void stop() {
        tools.stream().forEach(DownloadTool::stop);
    }
}
